<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="客户编号" prop="companyCode">
        <el-input
            v-model="queryParams.postCode"
            placeholder="请输入客户编号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户简称" prop="companyName">
        <el-input
            v-model="queryParams.postName"
            placeholder="请输入客户简称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户全称" prop="fullName">
        <el-input
            v-model="queryParams.postName"
            placeholder="请输入客户全称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>


      <el-form-item label="客户状态" prop="companyStatus">
        <el-select v-model="queryParams.companyStatus" placeholder="客户状态" clearable style="width: 200px">
          <el-option
              v-for="dict in company_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="付款方式" prop="paymentType">
        <el-select v-model="queryParams.paymentType" placeholder="请选择" clearable style="width: 200px">
          <el-option v-for="(item, index) in paymentTypeOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="approveStatus">
        <el-select v-model="queryParams.approveStatus" placeholder="请选择" clearable style="width: 200px">
          <el-option
              v-for="dict in company_audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['yanjing:company:add']"
        >添加客户</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="multiple"
            @click="handleBatchAudit"
            v-hasPermi="['yanjing:company:batchAudit']"
        >提交审核</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="multiple"
            @click="handleAuditTrue"
            v-hasPermi="['yanjing:company:batchAuditTrue']"
        >审核通过</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Edit"
            :disabled="multiple"
            @click="handleAuditFalse"
            v-hasPermi="['yanjing:company:batchAuditFalse']"
        >审核不通过</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Edit"
            :disabled="multiple"
            @click="handleBackUpdate"
            v-hasPermi="['yanjing:company:edit']"
        >下架修改</el-button>
      </el-col>



      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="companyCode" />
      <el-table-column label="客户全称" align="center" prop="fullName" />
      <el-table-column label="客户简称" align="center" prop="companyName" />
      <el-table-column label="付款方式" align="center" prop="paymentType" >
        <template #default="scope">
          <dict-tag :options="pay_type" :value="scope.row.paymentType" />
        </template>
      </el-table-column>
      <el-table-column label="付款比例" align="center" prop="paymentRatio" />
      <el-table-column label="客户状态" align="center" prop="companyStatus">
        <template #default="scope">
          <dict-tag :options="company_status" :value="scope.row.companyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="approveStatus" >
        <template #default="scope">
          <dict-tag :options="company_audit_status" :value="scope.row.approveStatus" />
        </template>
      </el-table-column>
      <el-table-column label="联系人姓名" align="center" prop="linkName" />
      <el-table-column label="联系电话" align="center" prop="linkMobile" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index) ">详细</el-button>
          <el-button link type="primary" v-if="scope.row.approveStatus==='CREATE'||scope.row.approveStatus==='FAIL'" icon="Edit" @click="handleBatchUpdate(scope.row)" >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDisable(scope.row)" >停用</el-button>
          <el-button link type="primary" v-if="scope.row.approveStatus==='CREATE'||scope.row.approveStatus==='FAIL'" icon="Delete" @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
<!--    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="companyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>-->
<!--
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Title">
-->
<!--
    <el-dialog :title="添加客户信息" v-model="open" width="1000px" append-to-body>
-->
      <el-dialog v-bind="$attrs" v-model="open" title="添加客户信息" append-to-body width="800px">
        <el-form ref="companyRef" :model="form" :rules="rules"  label-width="100px">
          <el-row>
              <el-col :span="24">
                <el-form-item label="客户全称" prop="fullName" >
                  <el-input v-model="form.fullName" placeholder="请输入客户全称" :maxlength="50" show-word-limit
                            clearable prefix-icon='el-icon-mobile' :style="{width: '100%'}" ></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="客户编号" prop="companyCode">
                <el-input v-model="form.companyCode" placeholder="请输入客户编号" readonly clearable
                          :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="客户简称" prop="companyName">
                  <el-input v-model="form.companyName" placeholder="请输入客户简称" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="付款方式" prop="paymentType">
                <el-select v-model="form.paymentType" placeholder="请选择付款方式" clearable
                           :style="{width: '100%'}">
                  <el-option v-for="(item, index) in paymentTypeOptions" :key="index" :label="item.label"
                             :value="item.value" :disabled="item.disabled"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="付款比例" prop="paymentRatio">
                <el-input v-model="form.paymentRatio" placeholder="请输入付款比例" clearable
                          :style="{width: '100%'}">
                  <template slot="append">%</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="审核状态" prop="approveStatus">
<!--                  <el-input v-model="form.approveStatus" placeholder="请输入审核状态" readonly clearable
                            :style="{width: '100%'}"></el-input>-->
                  <el-select v-model="form.approveStatus"  readonly clearable
                             :style="{width: '100%'}">
                    <el-option v-for="(item, index) in company_audit_status" :key="index" :label="item.label"
                               :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="客户状态" prop="companyStatus">
                <el-select v-model="form.companyStatus" placeholder="请选择客户状态" clearable
                           :style="{width: '100%'}">
                  <el-option v-for="(item, index) in companyStatusOptions" :key="index" :label="item.label"
                             :value="item.value" :disabled="item.disabled"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="联系人名字" prop="linkName">
                  <el-input v-model="form.linkName" placeholder="请输入联系人名字" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="联系人电话" prop="linkMobile">
                <el-input v-model="form.linkMobile" placeholder="请输入联系人电话" clearable
                          :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="业务员名字" prop="saleName">
                  <el-input v-model="form.saleName" placeholder="请输入业务员名字" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="业务员电话" prop="salePhone">
                <el-input v-model="form.salePhone" placeholder="请输入业务员电话" clearable
                          :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="显示品类" prop="genreId">
                  <el-checkbox-group v-model="form.genreId" >
                    <el-checkbox
                        v-for="(item,index) in genreIdOptions"
                        :key="item.value"
                        :label="item.value"
                                 :disabled="item.disabled">{{item.label}}
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
            <el-col :span="23">
                <el-form-item label="客户地址" prop="address">
                  <el-input v-model="form.address" placeholder="请输入客户地址" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
              <el-col :span="14">
                <el-form-item label="所在城市" prop="cityName">
                  <el-input v-model="form.cityName" placeholder="请输入所在城市" clearable
                            :style="{width: '100%'}"></el-input>
                </el-form-item>
              </el-col>
          </el-row>
        </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" v-if="isDetail"  @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script setup name="Post">
import {
  listCompany,
  getCompany,
  delCompany,
  addCompany,
  updateCompany,
  batchAudit,
  batchAuditSuccess,
  batchAuditFalse,
  disableCompany,
  backUpdate
} from "@/api/company/company.js";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const { company_status } = proxy.useDict("company_status");
const { company_audit_status } = proxy.useDict("company_audit_status");
const { pay_type } = proxy.useDict("pay_type");

const companyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isDetail = true;
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fullName: undefined,
    companyCode: undefined,
    companyName: undefined,
    companyStatus: "NORMAL",
    genreId: undefined,
    paymentType: undefined,
    paymentRatio: undefined,
    approveStatus: undefined,
    linkName: undefined,
    linkMobile: undefined,
    saleName: undefined,
    cityName: undefined,
    address: undefined,
    salePhone: undefined
  },
  rules: {
    fullName: [{required: true, message: "客户全称不能为空", trigger: "blur"}],
    companyName: [{required: true, message: "客户简称不能为空", trigger: "blur"}],
    companyStatus: [{required: true, message: "客户状态不能为空", trigger: "blur"}],
    paymentType: [{required: true, message: "支付方式不能为空", trigger: "blur"}],
    paymentRatio: [{required: true, message: "支付比例不能为空", trigger: "blur"}],
  },
  paymentTypeOptions: [{
    "label": "实时付款",
    "value": 0
  }, {
    "label": "信用付款",
    "value": 1
  }],
  companyStatusOptions: [{
    "label": "正常",
    "value": "NORMAL"
  }, {
    "label": "停用",
    "value": "CANCEL"
  }],
  genreIdOptions: [{
    "label": "视光版",
    "value": 7
  }, {
    "label": "医疗版",
    "value": 8
  }],
});

const { queryParams, form, rules,paymentTypeOptions,companyStatusOptions,genreIdOptions } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listCompany(queryParams.value).then(response => {
    companyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
  this.isDetail = true;

}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    paymentRatio: undefined,
    postSort: 0,
    companyStatus: null,
    remark: undefined
  };
  proxy.resetForm("companyRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.companyId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加岗位";
  this.isDetail = true;

}

function  handleBatchAudit(row) {
  const postIds = row.companyId || ids.value;
  proxy.$modal.confirm('是否提交审核所选客户信息？').then(function() {
    return batchAudit(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}

/**
 * 批量审核通过
 */
function handleAuditTrue(row) {
  const postIds = row.companyId || ids.value;
  proxy.$modal.confirm('是否审核通过所选客户信息？').then(function() {
    return batchAuditSuccess(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}

/**
 * 批量审核不通过
 */
function handleAuditFalse(row) {
  const postIds = row.companyId || ids.value;
  proxy.$modal.confirm('是否审核通过所选客户信息？').then(function() {
    return batchAuditFalse(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}

function handleBatchUpdate(row) {
  reset();
  this.isDetail = true;
  const companyId = row.companyId ;
  getCompany(companyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改";
  });
}


/**
 * 停用
 */
function handleStop() {

}
/**
 * 删除
 */
function handleDelete(row) {
  const postIds = row.companyId || ids.value;
  proxy.$modal.confirm('是否删除此客户信息').then(function() {
    return delCompany(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}


/** 下架按钮操作 */
function handleBackUpdate(row) {
  reset();
  const companyId = row.companyId || ids.value;
  proxy.$modal.confirm('是否下架所选客户').then(function() {
    return backUpdate(companyId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}
/** 提交按钮 */
function submitForm() {

  proxy.$refs["companyRef"].validate(valid => {
    if (valid) {
      let genreIds = "";
      for (let i in form.value.genreId) {
        genreIds = genreIds + form.value.genreId[i]+",";
      }
      form.value.genreId = genreIds.substring(0,genreIds.length-1);
      if (form.value.companyId != undefined) {
        updateCompany(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCompany(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleView(row) {
  open.value = true;
  form.value = row;
  this.isDetail = false;

}

/** 停用按钮操作 */
function handleDisable(row) {
  const postIds = row.companyId || ids.value;
  proxy.$modal.confirm('是否停用此客户信息').then(function() {
    return disableCompany(postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }).catch(() => {});
}
/** 导出按钮操作 */


getList();
</script>
